//
//  PicViewController.m
//  多玩盒子
//
//  Created by 刘胜 on 16/2/26.
//  Copyright © 2016年 刘胜. All rights reserved.
//

#import "PicViewController.h"
#import "TopModel.h"
#import "LSZNetWorkTools.h"
#import "PhotoCollectionViewCell.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)NSMutableArray * allArray;

@end

@implementation PicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    [self getData];
    [self creatCollection];
}

-(void)getData {
    NSString * url = @"http://box.dwstatic.com/apiAlbum.php?action=l&albumsTag=wallpaper&p=1&v=166&OSType=iOS9.1&versionName=2.5.1";
     NSString *encoded = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [LSZNetWorkTools getWithUrl:encoded parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSMutableArray * array = [result objectForKey:@"data"];
        for (NSMutableDictionary * dic in array) {
            TopModel * model = [TopModel modelWithDictionary:dic];
            [self.allArray addObject:model];
        }
        [_collectionView reloadData];
    } fail:^(NSError *error) {
        
        
    }];
    
}
-(void)creatCollection {
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake(170, HEIGHT/2);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //    flowLayout.minimumInteritemSpacing = 1;
    //    flowLayout.minimumLineSpacing = 1;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    cell.cellModel = self.allArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
